var Utils={
  Version: '1.0'
}


function replaceWithVariable(str,vs,ve,vn,v){
		var newStr = "";
		var vsu = new Utils.VariableStringUtil("");
		vsu.varPrefix = vs;
		vsu.varSuffix = ve;
		vsu.str = str;
		vsu.reSet();
		var lastIndex = 0;
		var hasVar = false;
		while (vsu.hasMoreVariables()) {
			hasVar = true;
			var va = vsu.nextVariable();
			if(vn == va){
				newStr += str.substring(lastIndex, vsu.currentStartIndex) + v;
				lastIndex = vsu.currentEndIndex + 1;
			}
		}
		if (hasVar)
			return newStr + str.substring(lastIndex);
		else
			return str;
}

function replaceWithVariable(str,vs,ve,vm){
		var newStr = "";
		var vsu = new Utils.VariableStringUtil("");
		vsu.varPrefix = vs;
		vsu.varSuffix = ve;
		vsu.str = str;
		vsu.reSet();
		var lastIndex = 0;
		var hasVar = false;
		while (vsu.hasMoreVariables()) {
			hasVar = true;
			var va = vsu.nextVariable();
			var v = vm.getByName(va);
			newStr += str.substring(lastIndex, vsu.currentStartIndex) + v;
			lastIndex = vsu.currentEndIndex + 1;
		}
		if (hasVar)
			return newStr + str.substring(lastIndex);
		else
			return str;
}

Utils.VariableStringUtil=function(str){
	this.str = str;
	this.varPrefix="{";
	this.varSuffix="}";
	this.reSet();
};
Utils.VariableStringUtil.prototype={
	
	reSet: function(){
		this.currentStartIndex=0;
		this.currentEndIndex=0;
	},
	
	hasMoreVariables: function(){
		var vsIndex = this.str.indexOf(this.varPrefix, this.currentEndIndex);
		var veIndex = this.str.indexOf(this.varSuffix, vsIndex);
		var hasNext = vsIndex != -1 && veIndex != -1;
		return hasNext;
	},
	
	nextVariable: function(){
		var vsIndex = this.str.indexOf(this.varPrefix, this.currentEndIndex);
		var veIndex = this.str.indexOf(this.varSuffix, vsIndex);
		var va = "";
		va = this.str.substring(vsIndex + this.varPrefix.length, veIndex);
		this.currentStartIndex = vsIndex;
		this.currentEndIndex = veIndex;
		return va;
	},
	
	getAllVariables: function(){
		var vars = [];
		if (this.str != undefined && this.str != null && this.str != "") {
			while (this.hasMoreVariables()) {
				vars[vars.length]=this.nextVariable();
			}
		}
		return vars;
	}
}


Utils.List = function(){
	this.values=[];
};
Utils.List.prototype={
	add:function(ele){
		this.values[this.values.length]=ele;
	},
	
	get:function(index){
		return this.values[index];
	},
	
	removeByIndex:function(index){
		if(index < 0 || index > this.values.length-1){
			return null;
		}else{
			var e = this.get(index);
			this.values.splice(index,1);
			return e;
		}
	},
	
	clear:function(){
		this.values.splice(0,this.values.length);
	},
	
	indexOf:function(ele){
		for(var i=0;i<this.values.length;i++){
			if(this.values[i] == ele){
				return i;
			}
		}
		return -1;
	},
	
	removeByElement:function(ele){
		return this.removeByIndex(this.indexOf(ele));
	},
	
	contains:function(ele){
		return this.indexOf(ele) != -1;
	},
	
	size:function(){
		return this.values.length;
	},
	
	toString: function(){
		var str = "";
		for(var i=0;i<this.size();i++){
			str += i==0?this.get(i) : ","+this.get(i);
		}
		return str;
	}
}

Utils.Map = function(){
	this.keys = [];
	this.values = [];
};
Utils.Map.prototype={
	
	put: function(key,value){
		var idx = this.indexOf(key);
		if(idx == -1){
			this.keys[this.keys.length] = key;
			this.values[this.values.length] = value;
		}else{
			this.values[idx] = value;
		}
	},
	
	containsKey: function(key){
		return this.indexOf(key) != -1;
	},
	
	getByName: function(key){
		var index = this.indexOf(key);
		if(index != -1){
			return this.values[index];
		}else{
			return null;
		}
	},
	
	getByIndex: function(index){
		return this.values[index];
	},
	
	remove: function(key){
		var index = this.indexOf(key);
		if(index != -1){
			this.keys.splice(index,1);
			return this.values.splice(index,1);
		}else{
			return null;
		}
	},
	
	indexOf: function(key){
		for(var m=0;m<this.keys.length;m++){
			if(key == this.keys[m]){
				return m;
			}
		}
		return -1;
	},
	
	clear: function(){
		this.keys.splice(0,this.keys.length);
		this.values.splice(0,this.values.length);
	},
	
	size: function(){
		return this.keys.length;
	}
}


Utils.Datacolumn=function(){
	this.name='';
	this.type='';
	this.value='';
};
Utils.Datacolumn.prototype={
	 
	 resolve: function(xmlDoc){
	 	this.name = xmlDoc.getAttribute("name");
	 	this.type = xmlDoc.getAttribute("type");
	 	if(xmlDoc.firstChild){
		 	this.value = xmlDoc.firstChild.nodeValue;
	 	}else{
	 		this.value = "";
	 	}
	 },
	 
	 toString: function(){
	 	return str = "<col name=\""+this.name+"\" type=\""+this.type+"\">"+this.value+"</col>";
	 },
	 
	 toParamStr: function(){
	 	return this.name+'='+this.value;
	 }
}


Utils.Datarow=function(){
	this.id=-1;
	this.cols = new Utils.Map();
};
Utils.Datarow.prototype={
	
	addCol: function (col){
		this.cols.put(col.name,col);
	},
	
	getColByName: function (name){
		return this.cols.getByName(name);
	},
	
	getColByIndex: function(index){
		return this.cols.getByIndex(index);
	},
	
	size: function(){
		return this.cols.size();
	},
	
	resolve: function(xmlDoc){
		this.id = xmlDoc.getAttribute("id");
		var colDocs = xmlDoc.getElementsByTagName("col");
		for(var m=0;m<colDocs.length;m++){
			var col = new Utils.Datacolumn();
			col.resolve(colDocs[m]);
			this.addCol(col);
		}
	},
	
	toString: function(){
		var str = "<row id=\""+this.id+"\" cols=\""+this.colCount+"\">";
		for(var m=0;m<this.size();m++){
			str += this.getColByIndex(m).toString();
		}
		str += "</row>";
		return str;
	},
	 
	 toParamStr: function(){
	 	var str = '';
	 	for(var i=0;i<this.size();i++){
	 		str += (i==0)? this.getColByIndex(i).toParamStr() : '&'+this.getColByIndex(i).toParamStr();
	 	}
	 	return str;
	 }
}

Utils.Dataset=function(){
		this.name = "data set";
		this.count = 0;
		this.page = 1;
		this.pageCount = 1;
		this.pageSize = 1;
		this.rows = new Utils.Map();
};
Utils.Dataset.prototype={

	addRow: function (row){
		this.rows.put(row.id,row);
	},
	
	getRowById: function(id){
		return this.rows.get(id);
	},
	
	getRowByIndex: function(index){
		return this.rows.getByIndex(index);
	},
	
	size: function(){
		return this.rows.size();
	},
	
	resolve: function(xmlDoc){
		this.name = xmlDoc.getAttribute("name");
		this.count = xmlDoc.getAttribute("count")?xmlDoc.getAttribute("count") : 0;
		this.page = xmlDoc.getAttribute("page")?xmlDoc.getAttribute("page") : 1;
		this.pageCount = xmlDoc.getAttribute("pageCount") ? xmlDoc.getAttribute("pageCount") : 1;
		this.pageSize = xmlDoc.getAttribute("pageSize") ? xmlDoc.getAttribute("pageSize") : 20;
		var rowDocs = xmlDoc.getElementsByTagName("row");
		for(var m=0;m<rowDocs.length;m++){
			var r = new Utils.Datarow();
			r.resolve(rowDocs[m]);
			this.addRow(r);
		}
	},
	
	toString: function(){
		var str = "<dataset name=\""+this.name+" count=\""+this.count+"\" page=\""+this.page+"\" pageCount=\""+this.pageCount+"\" pageSize=\""+this.pageSize+"\">";
		for(var m=0;m<this.size();m++){
			str += this.getRowByIndex(m).toString();
		}
		str += "</dataset>";
		return str;
	}
}